using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundMgr : XObjBase
{
    public static SoundMgr Instance;
    protected override void Awake()
    {
        base.Awake();
        Instance = this;
    }

    public AudioSource[] audioEffects;
    private int audioEffectIndex = -1;

    private AudioSource GetFreeAudioEffect()
    {
        var source = audioEffects[++audioEffectIndex % audioEffects.Length];
        return source;
    }
    public void PlayEffect(string effectName)
    {
        var name = XRes.N(effectName, XResType.AudioClip);
        this.LoadAssetAsync<AudioClip>(name, (audioClip) => {
            var source = GetFreeAudioEffect();
            source.clip = audioClip;
            source.Play();
        });
    }

    public void PlayEffect(string effectName, float timeOffset)
    {
        var name = XRes.N(effectName, XResType.AudioClip);
        this.LoadAssetAsync<AudioClip>(name, (audioClip) => {
            var source = GetFreeAudioEffect();
            source.clip = audioClip;
            source.PlayScheduled(timeOffset);
        });
    }
}
